package com.woniuxy.joylifeforum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.*;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifeforum.dto.TopicDto;
import com.woniuxy.joylifeforum.service.UserService;
import com.woniuxy.joylifeforum.utils.ForumRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.rmi.CORBA.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用于用户中心的相关操作  查询用户信息 粉丝 关注的人 等
 */
@Service
public class UserServiceIml implements UserService {
    @Autowired
    private ForumTopicMapper forumTopicMapper;
    @Autowired
    private ForumReplyMapper forumReplyMapper;
    @Autowired
    private CenterUserMapper centerUserMapper;
    @Autowired
    private ForumForumMapper forumForumMapper;
    @Autowired
    private ForumConcernMapper forumConcernMapper;
    @Autowired
    private ForumTopicPraiseMapper forumTopicPraiseMapper;
    @Autowired
    private ForumRedis forumRedis;
    @Autowired
    private ForumPhotoMapper forumPhotoMapper;
    @Autowired
    private ForumTopicCollectMapper forumTopicCollectMapper;
    @Autowired
    private ForumExpMapper forumExpMapper;
    @Autowired
    private ForumReplyPraiseMapper ForumReplyPraiseMapper;
    @Autowired
    private ForumTopicReportMapper forumTopicReportMapper;
    @Autowired
    private ForumReplyReportMapper forumReplyReportMapper;
    @Autowired
    private ForumAdminMapper forumAdminMapper;
    @Autowired
    private ForumFansMapper forumFansMapper;
    /*
    获取用户信息
     */
    @Override
    public Map<String, Object> findUser( String userId,String centerUser) {
        //用户
        CenterUser centerUser1 = centerUserMapper.selectByPrimaryKey(centerUser);
        //社区
        ForumForum forumForum = forumForumMapper.selectByPrimaryKey(centerUser1.getForumId());
        //用户粉丝数量
        ForumFansExample myFans=new ForumFansExample();
        myFans.createCriteria().andUserIdEqualTo(centerUser);
        long fansNum = forumFansMapper.countByExample(myFans);
        //关注用户数量
        ForumFansExample myFans2=new ForumFansExample();
        myFans2.createCriteria().andFansIdEqualTo(centerUser);
        long concernNum = forumFansMapper.countByExample(myFans2);
        //用户话题量
        ForumTopicExample topicExample=new ForumTopicExample();
        ForumTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andUserIdEqualTo(centerUser);
        criteria.andStatusEqualTo(0);
        long topicNum = forumTopicMapper.countByExample(topicExample);
        //用户关注社区量
        ForumConcernExample concernExample=new ForumConcernExample();
        concernExample.createCriteria().andUserIdEqualTo(centerUser);
        long forumNum = forumConcernMapper.countByExample(concernExample);

        Map<String,Object> result=new HashMap<>();
        //若是他人，判断用户是否关注
        if(!userId.equals(centerUser)){
            ForumFansExample fans=new ForumFansExample();
            ForumFansExample.Criteria criteria1 = fans.createCriteria();
            criteria1.andUserIdEqualTo(centerUser);
            criteria1.andFansIdEqualTo(userId);
            long status = forumFansMapper.countByExample(fans);
            result.put("status",status);
        }else{
            //自己，不用关注
            result.put("status",3);
        }
        //保存
        result.put("user",centerUser1);
        result.put("forumForum",forumForum);
        result.put("fansNum",fansNum);
        result.put("concernNum",concernNum);
        result.put("topicNum",topicNum);
        result.put("forumNum",forumNum);
        return result;
    }
    /*
    用户发送的话题
     */
    @Override
    public PageInfo<TopicDto> findSendTopic(String userId, String findUserId, Integer page, Integer limit) {
        //查询用户发送的话题
        ForumTopicExample topicExample=new ForumTopicExample();
        topicExample.setOrderByClause("date DESC");
        ForumTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andUserIdEqualTo(findUserId);
        criteria.andStatusEqualTo(0);
        //分页
        PageHelper.startPage(page, limit);
        List<ForumTopic> forumTopics = forumTopicMapper.selectByExampleWithBLOBs(topicExample);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria1 = photoExample.createCriteria();
            criteria1.andTopicIdEqualTo(forumTopic.getId());
            criteria1.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }
    /*
    查询我的收藏
     */
    @Override
    public PageInfo<TopicDto> findCollectTopic(String userId, String findUserId, Integer page, Integer limit) {
        //查询用户收藏的话题
        ForumTopicCollectExample topicCollect=new ForumTopicCollectExample();
        topicCollect.createCriteria().andUserIdEqualTo(findUserId);
        List<ForumTopicCollect> forumTopicCollects = forumTopicCollectMapper.selectByExample(topicCollect);
        List<String>collectStr=new ArrayList<>();
        collectStr.add("");
        for(ForumTopicCollect forumTopicCollect:forumTopicCollects){
            Integer status = forumTopicMapper.selectByPrimaryKey(forumTopicCollect.getTopicId()).getStatus();
            if(status==0){
                collectStr.add(forumTopicCollect.getTopicId());
            }
        }

        ForumTopicExample topicExample=new ForumTopicExample();
        topicExample.setOrderByClause("date DESC");
        ForumTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdIn(collectStr);
        criteria.andStatusEqualTo(0);
        //分页
        PageHelper.startPage(page, limit);
        List<ForumTopic> forumTopics = forumTopicMapper.selectByExampleWithBLOBs(topicExample);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria1 = photoExample.createCriteria();
            criteria1.andTopicIdEqualTo(forumTopic.getId());
            criteria1.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }
    /*
    用户的回复
     */
    @Override
    public PageInfo<Map<String, Object>> findReplyTopic(String userId, String findUserId, Integer page, Integer limit) {
        //检索出回复
        ForumReplyExample replyExample=new ForumReplyExample();
        replyExample.setOrderByClause("date DESC");
        ForumReplyExample.Criteria criteria = replyExample.createCriteria();
        criteria.andUserIdEqualTo(findUserId);
        criteria.andStatusEqualTo(0);
        PageHelper.startPage(page,limit);
        List<ForumReply> forumReplies = forumReplyMapper.selectByExampleWithBLOBs(replyExample);
        //遍历赋值
        List< Map<String,Object>>data=new ArrayList<>();
        for(ForumReply forumReply:forumReplies){
            //用户
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(findUserId);
            //被回复内容
            String content;
            if(forumReply.getReplyId()==null){
                //获取话题内容
                content = forumTopicMapper.selectByPrimaryKey(forumReply.getTopicId()).getContent();
            }else{
                //获取回复内容
                content = forumReplyMapper.selectByPrimaryKey(forumReply.getReplyId()).getContent();
            }
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumReply.getForumId());
            //保存
            Map<String,Object>map =new HashMap<>();
            map.put("forumReply",forumReply);
            map.put("centerUser",centerUser);
            map.put("content",content);
            map.put("forumForum",forumForum);
            data.add(map);
        }
        PageInfo<Map<String,Object>> dataHP=new PageInfo<>(data);
        return dataHP;
    }
    /*
    添加用户关注
     */
    @Override
    public Integer addConcerns(String userId,String targetUserId) {
        try {
            //判断是否存在
            ForumFansExample fansExample=new ForumFansExample();
            ForumFansExample.Criteria criteria = fansExample.createCriteria();
            criteria.andUserIdEqualTo(targetUserId);
            criteria.andFansIdEqualTo(userId);
            long l = forumFansMapper.countByExample(fansExample);
            if(l==0){
                //不存在
                String id = KeyUtil.generateKey();
                ForumFans fans=new ForumFans(id,targetUserId,userId);
                forumFansMapper.insert(fans);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }
    /*
    取消用户关注
     */
    @Override
    public Integer cancelConcerns(String userId,String targetUserId) {
        try {
            //删除
            ForumFansExample fansExample=new ForumFansExample();
            ForumFansExample.Criteria criteria = fansExample.createCriteria();
            criteria.andUserIdEqualTo(targetUserId);
            criteria.andFansIdEqualTo(userId);
            forumFansMapper.deleteByExample(fansExample);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }
}
